<template>
  <div class="user-form">
    <el-dialog title="添加用户资料" :visible.sync="addUserFormVisible" center>
      <el-form :model="form" :rules="rules" label-width="100px" ref="form">
        <el-form-item
          label="用户名"
          prop="username"
          v-if="status == 0 || status == 1"
        >
          <el-input
            v-model="form.username"
            @blur="veruser"
            :disabled="status == 1"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="密码"
          prop="password"
          v-if="status == 0 || status == 1"
        >
          <el-input v-model="form.password" type="password"></el-input>
        </el-form-item>
        <el-form-item
          label="姓名"
          prop="u_name"
          v-if="status == 0 || status == 2"
        >
          <el-input v-model="form.u_name"></el-input>
        </el-form-item>

        <el-form-item label="性别" prop="sex" v-if="status == 0 || status == 2">
          <el-radio-group v-model="form.sex">
            <el-radio label="男"></el-radio>
            <el-radio label="女"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="手机号"
          prop="phone"
          v-if="status == 0 || status == 2"
        >
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="微信号" v-if="status == 0 || status == 2">
          <el-input v-model="form.wechat"></el-input>
        </el-form-item>

        <el-form-item
          label="所属部门"
          prop="branch_id"
          v-if="status == 0 || status == 2"
        >
          <el-select v-model="form.branch_id" placeholder="请选择所属部门">
            <el-option
              v-for="item in deprtOptions"
              :key="item.branch_id"
              :label="item.branch_name"
              :value="item.branch_id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="用户权限"
          prop="permissions"
          v-if="status == 0 || status == 2"
        >
          <el-radio-group v-model="form.permissions">
            <el-radio label="1">管理员</el-radio>
            <el-radio label="2">巡视员</el-radio>
            <el-radio label="3">测试员</el-radio>
            <el-radio label="4">坐席员</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="submitForm('form')">立 即 提 交</el-button>
        <el-button type="primary" @click="resetForm('form')">重 置</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
//  AddUser,
import { verfUserName, updateUser, passReset } from "@/http";
import Qs from "qs";

export default {
  props: {
    deprtOptions: {
      type: Array,
      require: true,
    },
  },
  data() {
    var checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("手机号不能为空"));
      } else {
        const reg = /^1[3|4|5|7|8][0-9]\d{8}$/;
        if (reg.test(value)) {
          callback();
        } else {
          return callback(new Error("请输入正确的手机号"));
        }
      }
    };
    return {
      addUserFormVisible: false,
      status: 0, // 0 添加用户 1 修改账号密码 2 修改用户资料
      form: {
        username: "",
        password: "",
        u_name: "",
        branch_id: "",
        sex: "男",
        phone: "",
        wechat: "",
        permissions: "",
      },
      permissions: "",
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 2,
            max: 10,
            message: "长度在 2 到 10 个字符",
            trigger: "blur",
          },
        ],
        u_name: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 2, max: 5, message: "长度在 2 到 5 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "change" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        branch_id: [
          { required: true, message: "请选择所属部门", trigger: "change" },
        ],
        phone: [
          {
            required: true,
            message: "请输入正确的手机号",
            trigger: "blur",
            validator: checkPhone,
          },
        ],
        sex: [{ required: true, message: "请选择性别", trigger: "change" }],
        permissions: [
          { required: true, message: "请选择权限", trigger: "change" },
        ],
      },
    };
  },

  watch: {
    form(newval) {
      console.log(newval);
      this.form.permissions = String(newval.permissions);
    },
    status(newval) {
      if (newval == 0) {
        this.form = {};
      }
    },
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.status === 0) {
            let data = Qs.stringify(this.form);
            this.$axios
              .post("platform/management/userBranchManagement/insertUser", data)
              .then((res) => {
                if (res.data.result_code == 200) {
                  this.$parent.queryUser();
                  this.$message.success("添加成功！");
                  this.addUserFormVisible = false;
                }
              })
              .catch((err) => {
                console.log(err);
              });
          } else if (this.status === 2) {
            updateUser({
              user_id: this.form["user_id"],
              u_name: this.form["u_name"],
              sex: this.form["sex"],
              phone: this.form["phone"],
              wechat: this.form["wechat"],
              permissions: this.form["permissions"],
              branch_id: this.form["branch_id"],
            }).then((res) => {
              if (res.result_code == 200) {
                this.$parent.queryUser();
                this.$message.success("修改成功!");
                this.addUserFormVisible = false;
              } else {
                this.$message.warning("修改失败!");
              }
            });
          } else {
            console.log(12456);
            passReset({
              user_id: this.form["user_id"],
              username: this.form["username"],
              password: this.form["password"],
            }).then((res) => {
              res.result_code == 200
                ? (this.$message.success("密码修改成功!"),
                  (this.addUserFormVisible = false))
                : this.$message.warning("密码修改失败!");
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      console.log(222);
      this.$refs[formName].resetFields();
    },
    veruser() {
      if (this.form.username) {
        verfUserName({ username: this.form.username }).then((res) => {
          res.result_code == 200
            ? this.$message.success("用户名可用！")
            : (() => {
                this.$message.warning("用户名已存在!"),
                  (this.form.username = "");
              })();
        });
      }
    },
  },
};
</script>

<style lang="less" scoped>
.el-radio.is-bordered.is-checked {
  border-color: #5fb878;
  color: #5fb878;
}
.el-form-item__content .el-select {
  width: 100%;
}
</style>
